package com.school.base.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.school.base.dao.IAdminDao;
import com.school.base.model.Admin;
import com.school.common.constant.Constant;
import com.school.common.tags.PageBean;

/**
 * @author gaobiao
 * 
 */
public class AdminService {
	private static Logger logger = Logger.getLogger(AdminService.class);
	private IAdminDao adminDao;

	public IAdminDao getAdminDao() {
		return adminDao;
	}

	public void setAdminDao(IAdminDao adminDao) {
		this.adminDao = adminDao;
	}

	public Admin getAdminById(int id) {
		return adminDao.getAdminById(id);
	}

	public List<Admin> getAdminList(Admin admin, PageBean pageBean) {
		if (pageBean == null) {
			pageBean = new PageBean();
		}
		pageBean.setTotalRecordCount(adminDao.getTotalCount(admin));
		return adminDao.getAdminList(admin, pageBean.getOffset(), pageBean.getPageSize());
	}

	public void updateAdmin(Admin admin) throws Exception {
		if (admin == null) {
			logger.error("Inser admin fail:admin object is null");
			throw new Exception("添加管理员失败");
		}
		if (admin.getRealName() == null || admin.getRealName().isEmpty()) {
			logger.error("Inser admin fail:realName is empty");
			throw new Exception("真实姓名不能为空");
		}
		if (admin.getPwd() != null && admin.getPwd().length() > 0) {
			if (admin.getPwd().length() < Constant.USER_PASSWORD_MIN_LENGTH) {
				logger.error("Inser admin fail:pwd length less than " + Constant.USER_PASSWORD_MIN_LENGTH);
				throw new Exception("密码不能小于" + Constant.USER_PASSWORD_MIN_LENGTH + "个字符");
			}
		}
		adminDao.updateAdmin(admin);
	}

	public boolean checkLoginName(String loginName, int adminId) {
		return adminDao.checkLoginName(loginName, adminId);
	}

	public void deleteAdmin(int id) {
		adminDao.deleteAdmin(id);
	}

	public Admin check(String loginName, String pwd) throws Exception {
		return adminDao.check(loginName, pwd);
	}

	public void insertAdmin(Admin admin) throws Exception {
		if (admin == null) {
			logger.error("Inser admin fail:admin object is null");
			throw new Exception("添加管理员失败");
		}
		if (admin.getLoginName() == null || admin.getLoginName().isEmpty()) {
			logger.error("Inser admin fail:loginName is empty");
			throw new Exception("登录名不能为空");
		}
		if (!this.checkLoginName(admin.getLoginName(), admin.getId())) {
			logger.error("Inser admin fail:loginName is used");
			throw new Exception("登录名重复");
		}
		if (admin.getPwd() == null || admin.getPwd().isEmpty()) {
			logger.error("Inser admin fail:password is empty");
			throw new Exception("密码不能为空");
		}
		if (admin.getPwd().length() < Constant.USER_PASSWORD_MIN_LENGTH) {
			logger.error("Inser admin fail:pwd length less than " + Constant.USER_PASSWORD_MIN_LENGTH);
			throw new Exception("密码不能小于" + Constant.USER_PASSWORD_MIN_LENGTH + "个字符");
		}
		if (admin.getRealName() == null || admin.getRealName().isEmpty()) {
			logger.error("Inser admin fail:realName is empty");
			throw new Exception("真实姓名不能为空");
		}
		adminDao.insertAdmin(admin);
	}
}
